package com.xt.LibaryDeskManager.servlet;


import com.xt.LibaryDeskManager.dto.UserMessage;
import com.xt.LibaryDeskManager.service.MessageService;
import com.xt.LibaryDeskManager.service.impl.MessageServiceImpl;
import com.xt.LibaryDeskManager.utils.StrUtils;
import com.xt.LibaryDeskManager.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Map;

@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 获取所有公告数量
     */
    @ResponseBody
    @RequestMapping("/getCount")
    public Map getCount()throws Exception{
        int  count=messageService.getCount();
        return StrUtils.creatMap("count",count);
    }

    /**
     * 获得所有公告信息
     */
    @ResponseBody
    @RequestMapping("/getAllMessage")
    public Map getAllMessage(Integer page,Integer limit) throws Exception {
      return  messageService.getAllMessage(page,limit);
    }

    /**
     * 删除指定的公告信息
     */
    @ResponseBody
    @RequestMapping("/deleteMessage")
    public Map deleteMessage(Integer mid)throws Exception{
        int code=messageService.deleteMessage(mid);
        return StrUtils.creatMessage("code",code);
    }
    /**
     * 更新公告信息
     */
    @ResponseBody
    @RequestMapping("/updateMessage")
    public Map updateMessage(HttpServletRequest req)throws Exception{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String data=sdf.format(System.currentTimeMillis());
        MessageVo messageVo=new MessageVo(Integer.parseInt(req.getParameter("mid")),req.getParameter("title"),req.getParameter("mess"),"a",2,data);
        int code=messageService.updateMessage(messageVo);
        return StrUtils.creatMessage("code",code);
    }
    /**
     * 添加公告信息
     */
    @ResponseBody
    @RequestMapping("/addMessage")
    public Map addMessage(HttpServletRequest req)throws Exception{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        UserMessage userMessage= (UserMessage) req.getSession().getAttribute("user");
        String data=sdf.format(System.currentTimeMillis());
        MessageVo messageVo=new MessageVo(0,req.getParameter("title"),req.getParameter("mess"),userMessage.getId(),userMessage.getLevel(),data);
        int code=messageService.addMessage(messageVo);
        return StrUtils.creatMessage("code",code);
    }

}
